package com.zs.leymovie.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.R;
import com.zs.leymovie.bean.PosterDataEntity;

import java.util.List;

/**
 * Created by Administrator on 2015/8/19 0019.
 */
public class MovieRecyclerViewAdapter extends RecyclerView.Adapter<MovieRecyclerViewAdapter.RcViewHolder> {
    private List<PosterDataEntity> list;
    private OnItemClickLinstener onItemClickLinstener;
    private BitmapUtils bitmapUtils;
    private BitmapDisplayConfig config;
    private final Animation animation;
    public MovieRecyclerViewAdapter(Context context, List list) {
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        config = new BitmapDisplayConfig();
        config.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        config.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        animation = AnimationUtils.loadAnimation(
                context, R.anim.home_listview_anim);
        config.setAnimation(animation);
    }
    @Override
    public RcViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_layout, parent, false);
        RcViewHolder vh = new RcViewHolder(view);
        return vh;
    }


    @Override
    public void onBindViewHolder(RcViewHolder holder, int position) {
        PosterDataEntity itemData = list.get(position);
        bitmapUtils.display(holder.img,itemData.getSmall_poster_url(),config);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class RcViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @ViewInject(R.id.recycler_img)
        private ImageView img;

        public RcViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            ViewUtils.inject(this, itemView);

        }

        @Override
        public void onClick(View v) {
            if (onItemClickLinstener != null) {
                onItemClickLinstener.onItemClickLinstener(getLayoutPosition());
            }
        }
    }

    public interface OnItemClickLinstener {

        public void onItemClickLinstener(int position);

    }

    public void setOnItemClickLinstener(OnItemClickLinstener onItemClickLinstener) {
        this.onItemClickLinstener = onItemClickLinstener;
    }
}
